package top.goldenyear.porpoise.system.permission.model;

import com.jfinal.kit.Kv;
import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import cn.hutool.v7.core.text.StrUtil;
import top.goldenyear.porpoise.common.core.util.tree.BaseNode;

/**
 * @author airhead
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = false)
public class RouteNode extends BaseNode<Integer, RouteNode> {
  private Integer id;
  private Integer parentId;

  private String name;
  private String path;
  private String component;
  private Kv props;
  private MenuMeta meta;
  private List<RouteNode> children;

  public static RouteNode convert(Permission permission) {
    RouteNode routeNode = new RouteNode();
    routeNode.setId(permission.getId());
    routeNode.setParentId(permission.getParentId());
    routeNode.setName(permission.getRouteName());
    routeNode.setProps(Kv.create().setIfNotBlank("url", permission.getHref()));

    String path = permission.getRoutePath();
    if (StrUtil.isNotBlank(permission.getPathParam())) {
      path += permission.getPathParam();
    }
    routeNode.setPath(path);

    routeNode.setComponent(permission.getComponent());

    MenuMeta menuMeta = new MenuMeta();
    menuMeta.setTitle(permission.getName());
    menuMeta.setIcon(permission.getIcon());
    menuMeta.setOrder(permission.getSortRank());
    menuMeta.setHideInMenu("1".equals(permission.getHide()));
    menuMeta.setKeepAlive("1".equals(permission.getKeepAlive()));
    menuMeta.setActiveMenu(permission.getKeepAlive());
    menuMeta.setMultiTab("1".equals(permission.getMultiTab()));
    menuMeta.setFixedIndexInTab(permission.getFiexdIndexTab());
    routeNode.setMeta(menuMeta);

    return routeNode;
  }
}
